const webUrl = "http://9uv183hv4864.vicp.fun"
const wsUrl = "ws://9uv183hv4864.vicp.fun"
const noteBox = 0.55
const maxNoteImg = 1.5

//转换日期的方法
const getDate = function(timeStamp){
  const date = new Date(timeStamp);  
  const year = date.getFullYear();  
  const month = String(date.getMonth() + 1).padStart(2, '0');  
  const day = String(date.getDate()).padStart(2, '0');  
  
  // 拼接成目标格式  
  return `${year}-${month}-${day}`;
}
//转换日期加时间的方法
const getDateTime = function(timeStamp){
  const date = new Date(timeStamp);  
  const year = date.getFullYear();  
  const month = String(date.getMonth() + 1).padStart(2, '0');  
  const day = String(date.getDate()).padStart(2, '0');  
  const hours = String(date.getHours()).padStart(2, '0');  
  const minutes = String(date.getMinutes()).padStart(2, '0');   
  
  // 拼接成目标格式  
  return `${year}-${month}-${day} ${hours}:${minutes}`; 
}

const getUserMailAccount = function(){
  let curUser = uni.getStorageSync("curUser") || null
  if(curUser != null){
    return curUser.mailAccount
  }
  return null
}

const getUserId = function(){
  let curUser = uni.getStorageSync("curUser") || null
  if(curUser != null){
    return curUser.id
  }
  return null
}

const getToken = function(){
  let token = uni.getStorageSync("token") || null
  if(token != null){
    return token
  }
  return null
}

const logout = function(){
  uni.removeStorageSync("curUser")
  uni.removeStorageSync("token")
}

const login = function(res){
  //保存到
  logout()
  wx.setStorageSync("cookieKey", res.header["Set-Cookie"]);
  uni.setStorageSync("curUser",res.data.data.user)
  uni.setStorageSync("token",res.data.data.token)
}

const attUserReq = function(userId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/attention/attUser",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        userId: userId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });  
}

const disAttUserReq = function(userId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/attention/disAttUser",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        userId: userId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  }); 
}

const likeNoteReq = function(noteId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/note/likeNote",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: getUserId(),
        noteId: noteId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const  dislikeNoteReq = function(noteId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/note/dislikeNote",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: getUserId(),
        noteId: noteId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}


const starNoteReq = function(noteId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/note/starNote",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: getUserId(),
        noteId: noteId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const disStarNoteReq = function(noteId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/note/disStarNote",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: getUserId(),
        noteId: noteId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const likeCmtReq = function(commentId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/comment/likeCmt",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        userId: getUserId(),
        commentId: commentId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}
const dislikeCmtReq = function(commentId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/comment/dislikeCmt",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        userId: getUserId(),
        commentId: commentId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const likeReplyReq = function(replyId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/reply/likeReply",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        userId: getUserId(),
        replyId: replyId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}
const dislikeReplyReq = function(replyId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/reply/dislikeReply",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        userId: getUserId(),
        replyId: replyId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}


const likeTripNewReq = function(tripNewId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/tripNew/likeTripNew",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        tripNewId: tripNewId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}


const dislikeTripNewReq = function(tripNewId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/tripNew/dislikeTripNew",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': getToken()
      },
      data: {
        tripNewId: tripNewId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const getNoteDetailReq = function(noteId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/note/getNoteDetail",
      header:{
        'token': getToken()
      },
      data: {
        noteId: noteId
      },
      success: (res) => {
        console.log(res.data); 
        resolve(res)
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  }); 
}

const deleteNoteReq = function(noteId){
    return new Promise((resolve, reject) => {
      uni.request({
        url: webUrl + "/note/deleteNote",
        method:'POST',
        header:{
          'Content-Type': 'application/x-www-form-urlencoded',
          'token': getToken()
        },
        data: {
          noteId: noteId
        },
        success: (res) => {
          console.log(res.data);
          resolve(res)
        },
        fail: (err) => {
          console.error('请求失败:', err);
        }
      })
  }); 
}

const starTripNewReq = function(tripNewId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/tripNew/starTripNew",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        tripNewId: tripNewId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const disStarTripNewReq = function(tripNewId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/tripNew/disStarTripNew",
      method:'POST',
      header:{
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        tripNewId: tripNewId
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          resolve(res)
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  });   
}

const deleteTripNewReq = function(tripNewId){
    return new Promise((resolve, reject) => {
      uni.request({
        url: webUrl + "/tripNew/deleteTripNew",
        method:'POST',
        header:{
          'Content-Type': 'application/x-www-form-urlencoded',
          'token': getToken()
        },
        data: {
          tripNewId: tripNewId
        },
        success: (res) => {
          console.log(res.data);
          resolve(res)
        },
        fail: (err) => {
          console.error('请求失败:', err);
        }
      })
  }); 
}

const getTripNewDetailReq = function(tripNewId){
  return new Promise((resolve, reject) => {
    uni.request({
      url: webUrl + "/tripNew/getTripNewDetail",
      header:{
        "token": getToken()
      },
      data: {
        tripNewId: tripNewId
      },
      success: (res) => {
        console.log(res.data);
        resolve(res)
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  }); 
}
export default{
  webUrl,
  wsUrl,
  noteBox,
  maxNoteImg,
  getDate,
  getDateTime,
  getUserMailAccount,
  getUserId,
  getToken,
  login,
  logout,
  attUserReq,
  disAttUserReq,
  likeNoteReq,
  dislikeNoteReq,
  starNoteReq,
  disStarNoteReq,
  likeCmtReq,
  dislikeCmtReq,
  likeReplyReq,
  dislikeReplyReq,
  likeTripNewReq,
  dislikeTripNewReq,
  getNoteDetailReq,
  deleteNoteReq,
  starTripNewReq,
  disStarTripNewReq,
  deleteTripNewReq,
  getTripNewDetailReq,
}